var MENU_SETTING = (function() {
	
	// 递归创建菜单
	function buildOneMenu(menu, level) {
		var paddingLeftStyle = '';
		if (level > 1) { // 2级菜单后需要缩进
			paddingLeftStyle = 'padding-left: ' + (level - 1) * 30 + 'px';
		}
		
		var e = '';
		e += '<tr>';
			e += '<td';
				if (menu.childrens != null) {
					e += ' hasChilidren'
				}
				if (level > 1) {
					e += ' isChilidren'
				}
				if (paddingLeftStyle.length) {
					e += ' style="' + paddingLeftStyle + '"';
				}
			e += '>';
				e += menu.name
			e += '</td>';
			e += '<td class="text-center">';
					e += '<input name="minusMenu" type="checkbox" ' + (menu.visibility == 1 ? 'checked' : '') + ' value="' + menu.id + '">';
			e += '</td>';
		e += '</tr>';
		if (menu.childrens != null) { // 没有2级菜单
			++level;
			$.each(menu.childrens, function(idx, next) {
				e += buildOneMenu(next, level);
			});
		}
		return e;
	}
	
	function loadMenu(doctorUUID, menuType, $root) {
		$root.empty();
		$.post(
			'wncrm_minus_menu.do/settting/init',
			{
				doctorUUID: doctorUUID,
				menuType: menuType
			},
			function(data, status) {
				$('div#minusMenuModal').data('menutype', menuType); // 保存时获取menu type
				var menus = data.menus;
				// 菜单内容
				var e = '';
				$(menus).each(function(idx, menu) {
					e += buildOneMenu(menu, 1);
				});
				$(e).prependTo($root);
			}
		);
		
	}
	
	return {
		loadMenu: loadMenu
	}
})();

$(document).ready(function() {
	
	var $minusMenuModal = $('div#minusMenuModal');
	// 菜单权限设置
	$('section#org').on('click', 'a[name="menuMinus"]', function() {
		var $that = $(this);
		var $doctorProp = $that.closest('tr');
		var doctorName = $doctorProp.data('name');
		var doctorUUID = $doctorProp.data('doctoruuid');
		
		$minusMenuModal.find('span.doctorName').text(doctorName);
		$minusMenuModal.modal({ keyboard: true }).data('doctoruuid', doctorUUID);
	});
	
	// 加载modal
	$minusMenuModal.on('shown.bs.modal', function() {
//		var menuType = $minusMenuModal.find('div.user-tabs>ul.nav>li.tab.active>a').data('menutype');
//		var doctorUUID = $minusMenuModal.data('doctoruuid');
//		MENU_SETTING.loadMenu(doctorUUID, menuType, $minusMenuModal.find('table>tbody'));
		$minusMenuModal.find('div.user-tabs>ul.nav>li.tab>a[data-menutype="1"]').click();
	}).on('hidden.bs.modal', function() {
		$minusMenuModal.find('table>tbody').empty();
	});
	
	// 切换横众菜单
	$minusMenuModal.find('div.user-tabs>ul.nav>li.tab>a').on('click', function() {
		var $that = $(this);
		var menuType = $that.data('menutype');
		var doctorUUID = $minusMenuModal.data('doctoruuid');
		MENU_SETTING.loadMenu(doctorUUID, menuType, $minusMenuModal.find('table>tbody'));
	});
	
	// 保存
	$minusMenuModal.find('button[name="save"]').on('click', function() {
		var doctorUUID = $minusMenuModal.data('doctoruuid');
		var menuType = $minusMenuModal.data('menutype');
		var minusMenus = [];
		var $minusMenus = $minusMenuModal.find('input[name="minusMenu"]');
		if ($minusMenus.length) {
			$minusMenus.each(function(idx, minusMenu) {
				var $minusMenu = $(minusMenu);
				if (!$minusMenu.prop('checked')) {
					minusMenus.push($minusMenu.val());
				}
			});
		}
		WeKnow.loading();
		$.ajax({
	        type: "POST", 
	        url: "wncrm_minus_menu.do/settting/save", 
	        dataType: "json",
	        async: false,
	        data: {
	        	doctorUUID: doctorUUID,
	        	menuType: menuType,
	        	minusMenus: minusMenus
	        },
	        success: function(data) {
	        	if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
	        	if (data.errcode == 0) {
	        		WeKnow.successMsg('保存成功');
//	        		$minusMenuModal.modal('hide');
	        	}else{
					WeKnow.errorMsg(data.errormsg ? data.errormsg : "保存失败");
				}
	        },
	        error: function() {
	        	WeKnow.systemMsg('系统异常');
	        }
		});
		WeKnow.closeLoading();
	});
});